﻿using System;
using System.IO;
using System.Windows.Forms;

using DreamCube.Foundation.Basic.Enums;
using DreamCube.Foundation.Basic.Utility;

namespace BGN.PlugIn.Client.Tool
{
    static class Helper
    {
        /// <summary>
        /// 安装包的产品ID号，用于查找是否安装了ActiveX
        /// </summary>
        private static String setupProjectID = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{2A565013-2252-4329-8B6F-C80DB01A7720}";
        /// <summary>
        /// 这个是老版本的产品ID号
        /// </summary>
        private static String setupProjectID2 = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{C6C66815-D4C3-4EC6-8E0C-4031CA9D0D94}";
        private static String clientSetupRegistryKey = @"SOFTWARE\森森工作室";
        /// <summary>
        /// 缓存客户端安装的路径，免得每次都要读注册表
        /// </summary>
        private static String clientSetupPath = String.Empty;
        /// <summary>
        /// 需要删除的旧版本文件
        /// </summary>
        private static String[] oldFiles = {"SisenMESS_SinfoPlugIn.Client.dll",
                                            @"Abet_File\BasicObjects.dll",
                                            @"Abet_File\DreamCube.Foundation.Basic.dll",
                                            @"Abet_File\DreamCube.Foundation.DllImport.dll",
                                            @"Abet_File\DreamCube.Framework.Utilities.Remoting.dll",
                                            @"Abet_File\Interop.SHDocVw.dll",
                                            @"Abet_File\Microsoft.Office.Interop.Owc11.dll",
                                            @"Abet_File\SisenMESS_SinfoPlugIn.Common.dll",
                                            @"Abet_File\SisenMESS_SinfoPlugIn.Model.dll",
                                            @"Abet_File\SisenMESS_SinfoPlugIn.Remoting.dll",
                                            @"Abet_File\SisenMESS_SinfoPlugIn.xml",
                                            @"Abet_File\SisenMESS-Interface.dll"
                                           };

        /// <summary>
        /// 获取布谷鸟客户端的安装目录
        /// </summary>
        /// <returns></returns>
        public static String GetClientSetupPath()
        {
            try
            {
                if(String.IsNullOrEmpty(clientSetupPath))
                    clientSetupPath = MyRegistry.Basic.GetLocalMachineSubKeyPropertyValue(clientSetupRegistryKey, "SisenMESS").ToString();
                return clientSetupPath;
            }
            catch (Exception ex) 
            {
                MyLog.MakeLog(ex);
            }
            return String.Empty;
        }

        /// <summary>
        /// 判断是否安装了布谷鸟客户端
        /// </summary>
        /// <returns></returns>
        public static Boolean HasSetupBGNClient()
        {
            return !String.IsNullOrEmpty(GetClientSetupPath());
        }

        /// <summary>
        /// 由于客户端可能安装了旧版本的插件，所以要删除旧版本的插件
        /// </summary>
        public static void DeleteClientOldFile()
        {
            try
            {
                if (oldFiles != null) 
                {
                    String clientSetupPath = GetClientSetupPath();
                    for (Int32 i = 0; i < oldFiles.Length; i++) 
                    {
                        String filePath = Path.Combine(clientSetupPath, @"Plug_Ins\" + oldFiles[i]);
                        MyIO.TryDeleteFile(filePath);
                    }
                }
            }
            catch (Exception ex)
            {
                MyLog.MakeLog(ex);
            }
        }

        /// <summary>
        /// 判断是否安装了ActiveX客户端
        /// </summary>
        /// <returns></returns>
        public static Boolean HasSetupActiveX()
        {
            //如果纯在指定的Key值，则返回true
            Boolean result = MyRegistry.Basic.IsLocalMachineSubKeyExists(setupProjectID) || MyRegistry.Basic.IsLocalMachineSubKeyExists(setupProjectID2);
            return result;
        }

        /// <summary>
        /// 卸载客户端的ActiveX插件
        /// </summary>
        /// <returns></returns>
        public static void UninstallAtiveX()
        {
            try
            {
                String uninstallString = MyRegistry.Basic.GetLocalMachineSubKeyPropertyValue(setupProjectID, "UninstallString").ToString();
                if (!String.IsNullOrEmpty(uninstallString))
                {
                    MyCMD.RunCmd(new String[] { uninstallString });
                }
                uninstallString = MyRegistry.Basic.GetLocalMachineSubKeyPropertyValue(setupProjectID2, "UninstallString").ToString();
                if (!String.IsNullOrEmpty(uninstallString))
                {
                    MyCMD.RunCmd(new String[] { uninstallString });
                }
            }
            catch (Exception ex)
            {
                MyLog.MakeLog(ex);
            }
        }

        /// <summary>
        /// 执行安装ActiveX
        /// </summary>
        /// <returns></returns>
        public static Boolean SetupActiveX()
        {
            String filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"ActiveXFiles\setup.exe");
            if (File.Exists(filePath))
            {
                MyCMD.RunEXE(filePath,"", null);
                return true;
            }
            else
            {
                MessageBox.Show("安装包可能存在问题，请重新下载安装包！", "提醒");
                return false;
            }
        }   

        /// <summary>
        /// 开始复制文件
        /// </summary>
        public static Boolean CopyFile()
        {
            String copyFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ClientFiles");
            String clientSetupPath = GetClientSetupPath();
            String pluginFileDestinationPath = Path.Combine(clientSetupPath, "Plug_Ins");
            String abetFileDestinationPath = Path.Combine(clientSetupPath, @"Plug_Ins\Abet_File");
            String[] pluginFiles = null;//复制到客户端的Plug_Ins目录下的文件
            String[] abetFiles = null; //复制到客户端的Plug_Ins\Abet_File目录下面的文件
            if (MyIO.TryGetFiles(copyFilePath, out pluginFiles, "*.dll") && MyIO.TryGetFiles(Path.Combine(Path.Combine(copyFilePath, "Abet_File"), ""), out abetFiles, "*"))
            {
                String[] desFilesPath = null;
                String[] abetFilesPath = null;
                Boolean result = MyIO.TryFileCopy(pluginFiles, pluginFileDestinationPath, out desFilesPath, true, HandleExceptionInTry.ReturnAndMakeLog) &&
                                 MyIO.TryFileCopy(abetFiles, abetFileDestinationPath, out abetFilesPath, true, HandleExceptionInTry.ReturnAndMakeLog);

                if (!result)
                {
                    MessageBox.Show("安装过程出现了异常，请联系开发员！", "提醒");
                }
                return result;
            }
            else
            {
                MessageBox.Show("安装包可能存在问题，请重新下载安装包！", "提醒");
            }
            return false;
        }
    }
}
